package com.cps.platform.client.config;

import javax.ws.rs.HttpMethod;
import javax.ws.rs.core.Response;

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.log4j.Logger;
import org.springframework.http.MediaType;

import com.cps.platform.client.Constant;
import com.cps.platform.core.Mapbean;
import com.cps.platform.core.json.JsonParserUtil;
import com.cps.platform.core.util.ReadProperties;
import com.cps.platform.core.util.ValidateUtil;

/**
 * <p>
 * 文件名称: ConfigClient.java
 * </p>
 * <p>
 * 文件描述: 配置服务的客户端
 * <p>
 * 版权所有: 版权所有(C)2011
 * </p>
 * <p>
 * 公 司: 联通系统集成有限公司
 * </p>
 * <p>
 * 其他说明: 由于配置服务广泛使用，特将该服务的客户端实现写入core中
 * </p>
 * <p>
 * 完成日期：2012-12-7
 * </p>
 * 
 * @author ChenZhang
 */
public class ConfigClient {

	private static Logger log = Logger.getLogger(ConfigClient.class);
	private String baseAddress;
	public ConfigClient(){
		ReadProperties readProperties = new ReadProperties("client-config.properties");
		baseAddress = readProperties.readValue("service.base.address.config");
	}
	
	/**
	 * 获取服务的配置数据
	 * @param appId
	 * @param authToken
	 * @param serviceId
	 * @return
	 */
	public Mapbean getConfig(String appId, String authToken) {
		WebClient webClient = WebClient.create(baseAddress);
		String serviceAddress = "/configService/get/{appId}/{authToken}";
		
		String mapBeanJson = webClient.path(serviceAddress,appId,authToken)
			.accept(MediaType.APPLICATION_JSON_VALUE)
			.header("Content-Type", "application/json")
			.header("Cache-Control", "no-cache").get(String.class);
			//.invoke(HttpMethod.GET,"",String.class);
		
		log.info("[Client] appId:"+appId+", response:"+mapBeanJson);

		if(ValidateUtil.isNull(mapBeanJson)){
			return null;
		}else{
			return (Mapbean) JsonParserUtil.getObjectFromJson(mapBeanJson, Mapbean.class);
		}
	}
	
	/**
	 * 获取服务的配置数据
	 * @param appId
	 * @param authToken
	 * @param key
	 * @return
	 */
	public String getConfigByKey(String appId, String authToken, String key) {
		WebClient webClient = WebClient.create(baseAddress);
		String serviceAddress = "/configService/get/{appId}/{authToken}/{key}";
		
		String json = webClient.path(serviceAddress,appId,authToken,key)
			.accept(MediaType.APPLICATION_JSON_VALUE)
			.header("Content-Type", "application/json")
			.header("Cache-Control", "no-cache").get(String.class);
			//.invoke(HttpMethod.GET,"",String.class);
		
		return json;
	}
	
	/**
	 * 向服务中添加配置数据
	 * @param appId
	 * @param authToken
	 * @param serviceId
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean addConfig(String appId, String authToken,
			String key, String value) {
		WebClient webClient = WebClient.create(baseAddress);
		String serviceAddress = "/configService/save/{appId}/{authToken}/{key}/{value}";
		
		Response response = webClient.path(serviceAddress,appId,authToken,key,value)
			.accept(MediaType.APPLICATION_JSON_VALUE).put(null);
		
		int status = response.getStatus();
		log.info("[Client] appId:"+appId+", "+key+":"+value+", status:"+ status);
		
		return Constant.OK==status?true:false;
	}
	
	/**
	 * 从服务中移出该服务的配置数据
	 * @param appId
	 * @param authToken
	 * @param serviceId
	 * @param key
	 * @return
	 */
	public boolean removeConfig(String appId, String authToken,
			String key){
		WebClient webClient = WebClient.create(baseAddress);
		String serviceAddress = "/configService/remove/{appId}/{authToken}/{key}";
		
		Response response = webClient.path(serviceAddress,appId,authToken,key)
			.accept(MediaType.APPLICATION_JSON_VALUE).put(null);
		
		int status = response.getStatus();
		log.info("[Client] appId:"+appId+", key:"+key+", status:"+ status);
		
		return Constant.OK==status?true:false;
	}
	
	/**
	 * 移出该服务的所有配置数据
	 * @param appId
	 * @param authToken
	 * @param serviceId
	 * @return
	 */
	public boolean removeConfig(String appId, String authToken) {
		WebClient webClient = WebClient.create(baseAddress);
		String serviceAddress = "/configService/remove/{appId}/{authToken}";
		
		Response response = webClient.path(serviceAddress,appId,authToken)
			.accept(MediaType.APPLICATION_JSON_VALUE).delete();
		
		int status = response.getStatus();
		log.info("[Client] appId:"+appId+", status:"+ status);
		
		return Constant.OK==status?true:false;
	}
	
	/**
	 * 获取各个服务的服务器访问基本地址
	 * @return
	 */
	public Mapbean getServiceBaseAddress(){
		WebClient webClient = WebClient.create(baseAddress);
		String serviceAddress = "/configService/address";
		
		String mapBeanJson = webClient.path(serviceAddress)
			.accept(MediaType.APPLICATION_JSON_VALUE).header("Content-Type", "application/json")
			.invoke(HttpMethod.GET,"",String.class);
		
		log.info("[Client] response:"+mapBeanJson);

		return (Mapbean) JsonParserUtil.getObjectFromJson(mapBeanJson, Mapbean.class);
	}
}
